<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
  <table border="1px">
    <tr>
      <td>日</td>
      <td>一</td>
      <td>二</td>
      <td>三</td>
      <td>四</td>
      <td>五</td>
      <td>六</td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
      <td></td>
    </tr>
  </table>
    <script>
      let tr = document.querySelectorAll('tr')
      console.log(tr[0].children[3].innerHTML);
      var month, year;
      year = parseInt(prompt("请输入年份"));
      month = parseInt(prompt("请输入月份")) - 1;
      let data = new Date(year, month, 0);
      data = data.getDate(); //获取一个月的天数
      var Fweek = new Date(year, month, 1).getDay(); //获取这个月的1号是第几个星期
      // 日历的行数
      var hang = 5;
      var first = 1;
      if (data == 28 && Fweek == 1) hang = 4;
      if (data == 30 && Fweek == 6) hang = 6;
      if (data == 31 && Fweek >= 5) hang = 6;
      for (var i = 0; i < hang; i++) {
        for (var j = 0; j < 7; j++) {
          if (i == 0 && j < Fweek) {
            tr[i+1].children[j].innerHTML = ' '
          } else {
            tr[i+1].children[j].innerHTML = first++
          }
          if (first >= data) {
            break;
          }
        }
      }
      
      // console.log(str);
      // 插入思维
      // let arr = [];
      // while (true) {
      //   let name = prompt("请输入歌名");
      //   if (name == "quit") break;
      //   if (arr.length == 0) {
      //     arr.push(name);
      //   } else {
      //     var len = arr.length;
      //     // 数组长度在不断的变化
      //     // for (var i = 0; i < len; i++) {
      //     //   if (arr[i] >= name) {
      //     //     arr.splice(i, 0, name);
      //     //     break;
      //     //   } else if (i === len - 1) arr.push(name);
      //     // }
      //     for (var i = 0; i < len; i++) {
      //       if (arr[i] >= name) {
      //         break;
      //       }
      //     }
      //     arr.splice(i, 0, name);
      //   }
      // }
      // console.log(arr);
    </script>
  </body>
</html>
